/**
 * 
 * 打开新页面
 * caption  页面标题
 * icon  页面图标
 * path  页面路径
 * 
 */
window.openTab = function (caption, icon, path) {
	if (!path) {
		return;
	}
	var tab = tabControl.getTab(path);
	if (!tab) {
		tab = {
			$type: "IFrame",
			name: path,
			caption: caption,
			iconClass: icon,
			path: path,
			closeable: true
		}
		tab = tabControl.addTab(tab);
	} else {
		tab.getControl().reload();
	}
	tabControl.set("currentTab", tab);
};

/**
 * 
 * 刷新页面
 * path  页面路径
 * 
 */
window.refreshTab = function (path) {
	var tab = tabControl.getTab(path);
	if (tab) {
		tab.getControl().reload();
	}
	
}

/**
 * 
 * 关闭当前页面
 * 
 */
window.closeTab = function () {
	var tab = tabControl.get("currentTab");
	if (tab) {
		tabControl.closeTab(tab);
	}	
};

/**
 * 
 * 层级按钮展开树表格方法
 * levelBtns  层及按钮
 * btnIndex  当前按钮
 * dsTreeGrid  表格dataSet
 * treeGrid  树表格
 * divs  树表格的滚动条【防止表格错位】
 * 
 */
window.expandNode = function(levelBtns, btnIndex, dsTreeGrid, treeGrid, divs) {
	levelBtns.each(function(btn) {
		if(btn.get("caption") == btnIndex) {
			btn._dom.style.backgroundColor = "#7986A9";
			btn._dom.style.color = "#FFF";
		} else {
			btn._dom.style.backgroundColor = "#F3F3F3";
			btn._dom.style.color = "#999999";
		}
	});
	var entitys = dsTreeGrid.getData().toJSON({
		includeDeletedEntity: true,
		generateState: true,
		generateOldData: true
	});
	dsTreeGrid.clear();
	dsTreeGrid.setData(entitys);
	treeGrid.get("bindingConfigs")[0].expandLevel = btnIndex - 1;
	setTimeout(function() {
		if (divs && divs.length > 0) {
			$(divs[0]).animate({scrollTop: 1}, 50);
		}
		treeGrid.refresh();
	}, 55);
};

/**
 * 
 * 展开收起后定位到上一条修改过的数据
 * treeGrid  表格
 * nodeCode  当前数据唯一标识值
 * property  当前数据唯一标识属性
 * 
 */
window.findNode = function(treeGrid, nodeCode, property) {
	var nodes = treeGrid.get("nodes");
	nodes.each(function(node) {
		if (node.get("data").get(property) == nodeCode) {
			treeGrid.set("currentNode", node);
		}
		var subNodes = node.get("nodes");
		findNode(subNodes);
	});
	
	if (nodeCode) {
		var frameTab = tabControl.getTab(window.oldCurrentNode.get("data.url"));
		var frameWindow = frameTab.get("control.iFrameWindow");
		if (frameWindow.viewMain && frameWindow.viewMain.entityStateChange) {
			frameWindow.viewMain.localEntity();
		}
	}
	
	function findNode(subNodes) {
		subNodes.each(function(subNode) {
			if (subNode.get("data").get(property) == nodeCode) {
				treeGrid.set("currentNode", subNode);
			}
			if (subNode.get("nodes")) {
				findNode(subNode.get("nodes"));
			}
		});
	}
}

//子页面 -----------定位当前行-------------------------------------------------
var divs = $("#d_treeGrid div");
$(divs[0]).scroll(function() {
	if (window.nodeCode) {
		var winHeight = $(window).height() * 0.65;
		if ($(".current-row") && $(".current-row").offset()) {
			var currentRowTop = $(".current-row").offset().top
			if ((currentRowTop - winHeight) < 50) {
				$(divs[0]).stop();
			}
		}
	}
});
//父页面回调定位当前行方法
view.localEntity = function() {
	setTimeout(function() {
		if (window.nodeCode && $(".current-row") && $(".current-row").offset()) {
			var divs = $("#d_treeGrid div");
			var tabelHeight = $(".data-table").css("height").split("px")[0];
			$(divs[0]).animate({scrollTop: tabelHeight * 2}, 3000);
		}
	}, 300);
}
//子页面 -----------定位当前行------------------------------------------------


/**
 * 
 * 页面存在编辑， 刷新、关闭页面、浏览器提示
 * 
 */
window.addEventListener("beforeunload", function(event) {
	var frameTab = tabControl.getTab(window.oldCurrentNode.get("data.url"));
	var frameWindow = frameTab.get("control.iFrameWindow");
	if (frameWindow.viewMain && frameWindow.viewMain.entityStateChange) {
		frameWindow.viewMain.entityStateChange();
	}
    if (window.entityStateChange) {
    	(event || window.event).returnValue = "";
    }
    return "";
});

/**
 * 
 * 提示内容长度为20方法
 * value  提示内容
 * 
 */
window.buildTipLength = function(specialtitle) {
	var result = "";
	if (specialtitle) {
		var titles = specialtitle.split(",");
		titles.remove("null");
		for (var i = 0; i < titles.length; i++) {
			if (titles[i].length > 20) {
				var num = titles[i].length / 20;
				var rem = titles[i].length % 20;
				if (rem == 0) {
					for (var j = 0; j < num; j++) {
						result += titles[i].substring(j * 20, (j * 20) + 20);
						if (j != (num - 1)) {
							result += "\n\t&nbsp;";
							if (navigator.userAgent.indexOf('Mac OS X') != -1) {
								result += "\t&nbsp;&nbsp;&nbsp;";
							}
						}
					}
				} else {
					for (var j = 0; j < num; j++) {
						if (j == (num - 1)) {
							result += titles[i].substring((j + 1) * 20, titles[i].length);
						} else {
							result += titles[i].substring(j * 20, (j * 20) + 20) + "\n\t&nbsp;";
							if (navigator.userAgent.indexOf('Mac OS X') != -1) {
								result += "\t&nbsp;&nbsp;&nbsp;";
							}
						}
					}
				}
			} else {
				result += titles[i];
			}
		}
	}
	return result;
}

/**
 * 
 * 数据格式化
 * input  需要格式化的值
 * 
 */
window.formatNumber = function(input) {
	if (input) {
		var temp = dorado.util.Common.formatFloat(input, "0.00000000");
		if (temp) {
			return dorado.util.Common.parseFloat(temp);
		} else {
			return 0;
		}	
	} else {
		return 0;
	}
};

/**
 * 
 * 拦截抛出异常并提示
 * 
 */
dorado.Exception.processException = function (e) {
	//window.parent.commonTipShow(e.message);
}

/**
 * 
 * 标题换行显示
 * caption  标题
 * 
 */
window.lineFeedCaption = function(caption) {
	if (caption) {
		var arr = caption.split(".");
		var titlePre = "";
		var titleSuf = "";
		for (var i = 0; i < arr.length; i++) {
			if (i == 0) {
				titlePre = arr[i];
			} else {
				if ((i + 1) != arr.length) {
					titleSuf = arr[i] + "." + arr[i + 1]
					break;
				} else {
					titleSuf = arr[i]
				}
			}
		}
		return titlePre + "\n" + titleSuf;
	}
}


//子页面----------------IFrame页面滚动问题-----------------------------------
var rotate = function(e) {
    var ee = e || window.event;
    if (ee.wheelDelta) { //IE、Opera、Chrome   
    
    	if (ee.wheelDelta < 0) { //当滑轮向上滚动时  
    		$(window.parent.viewMain._dom.childNodes[0]).animate({scrollTop: "+=12"}, 10);
    	} else if (ee.wheelDelta > 0) {	//当滑轮向下滚动时  
    		$(window.parent.viewMain._dom.childNodes[0]).animate({scrollTop: "-=12"}, 10);
    	}
    } else if (ee.detail) { //Firefox    
    
    	if (ee.detail < 0) {
    		$(window.parent.viewMain._dom.childNodes[0]).animate({scrollTop: "+=12"}, 10);
    	} else if (ee.detail > 0) {
    		$(window.parent.viewMain._dom.childNodes[0]).animate({scrollTop: "-=12"}, 10);
    	}
    }
}           
if (document.addEventListener) {
    document.addEventListener('DOMMouseScroll', rotate, false);
}
window.onmousewheel = document.onmousewheel = rotate;

/**
 * 
 * 操作父页面容器高度
 * 
 */
function addHeight() {
	var parentView = window.parent.viewMain;
	var iFrameConHeight = parentView.get("#iFrameCon").get("height");
	iFrameConHeight += 36;
	parentView.get("#iFrameCon").set("height", iFrameConHeight);
}

//子页面----------------IFrame页面滚动问题-----------------------------------


/**
 * 
 * 父级汇总
 * entity  改变的对象
 * property  改变的属性
 * notDataTypeParent  不是entity类型的父级
 * 
 */
window.sumParentValue = function(entity, property, notDataTypeParent){
	var parentEntity = entity.parent.parent;
	if (parentEntity && parentEntity.dataType._name != notDataTypeParent) {
		var sumValue = 0;
		parentEntity.get("children").each(function(sub){
				sumValue += window.formatNumber(sub.get(property));
		});
		parentEntity.set(property, sumValue)
		window.sumParentValue(parentEntity, property, notDataTypeParent);
	}
};

/**
 * 
 * entity校验是否通过
 * 
 */
window.validateEntity = function(entityList) {
	entityList.each(function(entity) {
		if (entity.validate() != "ok") {
			return false;
		}
		if (entity._data.children && entity.get("children").entityCount > 0) {
			window.validateEntity(entity.get("children"));
		}
	});
};

/**
 * 
 * 数据状态发生改变
 * 
 */
window.validateEntityState = function(entityList) {
	entityList.each(function(entity) {
		if (entity.state != 0) {
			window.entityStateChange = true;
			return;
		}
		if (entity._data.children && entity.get("children").entityCount > 0) {
			window.validateEntityState(entity.get("children"));
		}
	});
}





























